package com.example.restatr.Atcivity.Main;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.os.Bundle;
import android.os.Looper;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.restatr.Atcivity.Main.Fragment.UserFragment;
import com.example.restatr.R;
import com.example.restatr.Tools;
import com.google.android.material.button.MaterialButton;

import org.json.JSONException;
import org.json.JSONObject;

public class FeedBackAt extends AppCompatActivity {
    private Toolbar tb;
    private TextView barTitle;
    private EditText search;
    private EditText yj_content;
    private EditText yj_Title;
    private MaterialButton button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_yjkf_at );

        tb = findViewById( R.id.toolbar);
        barTitle = findViewById( R.id.bar_Title );
        search = findViewById( R.id.search );
        yj_content = findViewById( R.id.yj_content );
        button = findViewById( R.id.button );
        yj_Title = findViewById( R.id.yj_title );

        Tools.SetBarTitle( tb,barTitle,"意见反馈",search, FeedBackAt.this ,false);

        button.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String YJ_Content = yj_content.getText().toString();
                final String Yj_Title = yj_Title.getText().toString();
                if (Yj_Title.trim().length() == 0) {
                    Toast.makeText( FeedBackAt.this, "问题描述不能为空！~", Toast.LENGTH_SHORT ).show();
                } else if (YJ_Content.trim().length() == 0) {
                    Toast.makeText( FeedBackAt.this, "您还没有输入您的问题噢！~", Toast.LENGTH_SHORT ).show();
                } else {
                    new Thread( new Runnable() {
                        @Override
                        public void run() {
                            try {
                                JSONObject jsonObject = new JSONObject();
                                jsonObject.put( "content", YJ_Content );
                                jsonObject.put( "title", Yj_Title );
                                String post = Tools.Post( "/prod-api/api/common/feedback", UserFragment.token, jsonObject.toString() );
                                if (post != null) {
                                    JSONObject jieXi = new JSONObject( post );
                                    Looper.prepare();
                                    Toast.makeText( FeedBackAt.this, jieXi.getString( "msg" ), Toast.LENGTH_SHORT ).show();
                                    Looper.loop();
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        }
                    } ).start();

                }
            }
        } );
    }
}